package com.edais.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

public class BeanUtil {
	private static final Logger logger = Logger.getLogger(BeanUtil.class);
	
	// javaBean 转换到 Map
	public static Map<String,Object> getMapFromBean(Object obj){
		
		if(obj == null){
			return null;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for(PropertyDescriptor propertyDescriptor : propertyDescriptors){
				String key = propertyDescriptor.getName();
				
				// 过滤class属性
				if(!key.equals("class")){
					// 得到property对应的getter方法
					Method getter = propertyDescriptor.getReadMethod();
					Object value = getter.invoke(obj);
					
					map.put(key, value);
				}
			}
		}catch(Exception e){
			logger.debug("javaBean 转换到 Map 失败");
		}
		
		return map;
	}
	
	// Map 转换到 javaBean 
	public static void getBeanFromMap(Map<String,Object> map, Object obj){
		if(obj == null || map == null){
			return;
		}
		try{
			BeanUtils.populate(obj, map);
		}catch (Exception e) {
			// TODO: handle exception
			logger.debug("Map 转换到 javaBean 失败");
		}
	}
	
	

}
